import Vue from 'vue'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import routes from './config/routes'
import store from './store/'
//import VueI18n from 'vue-i18n'//国际化组件
import components from './components/' //加载公共组件
import iView from 'iview';
    //import { isJSON } from 'is-json';
import $ from 'jquery';
import './less/common.less';
Object.keys(components).forEach((key) => {
    var name = key.replace(/(\w)/, (v) => v.toUpperCase()) //首字母大写
    Vue.component(`v${name}`, components[key])
})
var fundebug = require("fundebug-javascript");
fundebug.apikey = "4dc358d9a101d10ce6f36e84d1a2538ddf1ba16b5731b2b5a402ff9d99cc5d96";
import global from './config/global'
Vue.use(VueRouter)
Vue.use(VueResource)
import VueQuillEditor from 'vue-quill-editor'
Vue.use(VueQuillEditor)
Vue.use(iView);
import VueLoading from './js/loading'
Vue.use(VueLoading);
import { mapActions } from 'vuex';


//国际化组件
// Vue.use(VueI18n)
// const i18n = new VueI18n({
//     locale:"zh-CN",//语言标识
//     messages:{
//         'zh-CN':require('./js/lang/zh'), //中文语言包
//         'en-US':require('./js/lang/en') //英文语言包
//     }
// })

//图片懒加载
import VueLazyload from 'vue-lazyload'
Vue.use(VueLazyload,{
    loading:require("./images/qx4-2.png")
})
const router = new VueRouter({
    routes,
    mode: 'history', 
})
function formatComponentName(vm)
{
  if (vm.$root === vm) return 'root';

  var name = vm._isVue ? (vm.$options && vm.$options.name) || (vm.$options && vm.$options._componentTag) : vm.name;
  return (name ? 'component <' + name + '>' : 'anonymous component') + (vm._isVue && vm.$options && vm.$options.__file ? ' at ' + (vm.$options && vm.$options.__file) : '');

}

Vue.config.errorHandler = function(err, vm, info)
{
  var componentName = formatComponentName(vm);
  var propsData = vm.$options && vm.$options.propsData;

  fundebug.notifyError(err,
  {
      metaData:
      {
          componentName: componentName,
          propsData: propsData,
          info: info
      }
   });
};
Vue.directive('drag', {
        inserted: function(el, binding) {
        var oDiv = el;
        var divparent=el.parentNode;
        oDiv.onmousedown = function(e){
            var disX = e.clientX -divparent.offsetLeft;
            var disY = e.clientY - divparent.offsetTop;
            document.onmousemove = function(e){
                var l = e.clientX-disX;
                var t = e.clientY-disY;
                if(l<=0){
                    l=0;
                }
                if(t<=0){
                    t=0;
                }
                if(divparent.offsetHeight+t>=document.documentElement.clientHeight){
                    t=document.documentElement.clientHeight-divparent.offsetHeight
                }
                if(divparent.offsetWidth+l>=document.documentElement.clientWidth){
                    l=document.documentElement.clientWidth-divparent.offsetWidth
                }
                divparent.style.left =  l+'px';
                divparent.style.top = t+'px';
            };
            document.onmouseup = function(){
                document.onmousemove=null;
                document.onmouseup=null;
            };
        }
    }
})
Vue.directive('resize', {
　　　　　inserted: function(el, binding) {
            var temp=binding.rawName.split('.');
            el.onclick=function(e){
                var a=binding.rawName.split('.');
                //$(".resize-panel").remove();
                var temp=binding.rawName.split('.');
                if($(".resize-panel").css("zIndex")=="0"&&$(el).prev().attr("class")=="resize-panel")return;
                new ZResize({
                    item: el, //可缩放的类名
                    binding:binding,
                    warpdiv:a[1]//当前外层的CLASS名
                });
                if(temp.length==3){
                    binding.value._self.textdblclick=temp[2];
                }
                else if(temp.length==5){
                    binding.value._self.textdblclick=$(".resize-panel").next().attr("index")+temp[4];
                }
               
                $(this).prev().attr('tabindex', 1); //首先为DIV标签添加tabindex属性  
                $(this).prev().focus();
                //binding.value._self.$set(binding.value._self[temp[1]],temp[2],"adsfa")//自定义指令双向数据绑定
            }
        }
})

Vue.http.options.credentials=false;
Vue.http.options.emulateJSON = true;          //Content-Type类型
//Vue.http.headers.common.Authorization = `Bearer ${jwtToken.access_token}`;
router.beforeEach(({meta, path}, from, next) => {
    var { auth = true } = meta
    var isLogin = Boolean(store.state.user.accountId) //true用户已登录， false用户未登录
    var dqtime = (new Date()).valueOf();
    var istoken=parseInt(store.state.user.expires_in)<dqtime
    
    if (auth && !isLogin && path !== '/index') {
        return next({ path: '/index' })
    }
    // if(auth && istoken&& path !== '/index'){//判断是否登录失效
    //     return next({ path: '/index' })//跳转传记页面
    // }
    next()
})



Vue.http.interceptors.push((request, next) => {
        // ...
        // 请求发送前的处理逻辑
        // ...
    next((response) => {
        if (response.body === 3) {
            // Update your state
            localStorage.removeItem('user');            
            window.location.reload();
        }
        return response
        // ...
        // 请求发送后的处理逻辑
        // ...
        // 根据请求的状态，response参数会返回给successCallback或errorCallback
        //return response
    })
})
new Vue({ store, router }).$mount('#app')